<template>
  <div class="home-containser">
    <el-container>
      <el-header>
        <div class="left">
          <img src="@/assets/heima-logo.png" alt="" style="float: left" />
          <div class="title">电商后台管理系统</div>
        </div>
        <div class="right">
          <el-button type="info">退出</el-button>
        </div>
      </el-header>
      <el-container>
        <el-aside :width="collapse?'70px':'200px'">
          <el-menu
            :default-active="defaultActive"
            class="el-menu-vertical-demo"
            @open="handleOpen"
            @close="handleClose"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#334ccf"
            :unique-opened="true"
            :collapse="collapse"
          >
            <div class="top-bar" @click="collapse=!collapse">| | |</div>
            <el-submenu :index="index1+''" v-for="(item1,index1) in menusList" :key="item1.id">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span>{{ item1.authName }}</span>
              </template>
              <el-menu-item-group>
                <el-menu-item
                  :index="item2.path"
                  v-for="item2 in item1.children"
                  :key="item2.id"
                  @click="getPath(item2.path)"
                  >{{ item2.authName }}</el-menu-item
                >
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
import { menus } from '@/api/login.js'
export default {
  name: 'Home',
  data () {
    return {
      // 存储菜单数据
      menusList: [],
      // 默认是false就是展开 false展开 true 折叠
      collapse: false,
      // 取出默认激活项
      defaultActive: localStorage.getItem('defaultPath') || 'roles'
    }
  },
  created () {
    this.getMenus()
  },
  methods: {
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    },
    // 获取菜单数据
    async getMenus () {
      const res = await menus()
      this.menusList = res.data.data
    },
    // 获取每一项当前的path值
    getPath (path) {
      // 存储path值到本地
      localStorage.setItem('defaultPath', path)
      // 赋值给默认项
      this.defaultActive = path
    }
  }
}
</script>
<style lang='less' scoped>
.home-containser {
  height: 100%;
  .el-container {
    height: 100%;
    .el-aside {
      height: 100%;
      background-color: #333744;
      color: #333;
      text-align: center;
      // line-height: 200px;
      .el-menu {
        border: none;
        .top-bar {
          font-size: 14px;
          color: #fff;
          cursor: pointer;
        }
        .el-submenu {
          text-align: left;
          ul.el-menu {
            .el-menu-item-group {
              .el-menu-item {
                text-align: center;
              }
              .el-menu-item-group__title {
                padding: 0;
              }
            }
          }
        }
      }
    }
  }
  // background-color: pink;
  .el-header {
    display: flex;
    justify-content: space-between;
    // align-items: center;
    background-color: #b3c0d1;
    color: #333;
    text-align: center;
    line-height: 60px;
    .left {
      .title {
        float: left;
        font-size: 22px;
        color: #fff;
        margin-left: 15px;
      }
    }
  }

  .el-main {
    background-color: #e9eef3;
    color: #333;
    text-align: center;
    line-height: 160px;
  }
}
body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>
